package com.animee.tally;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.animee.tally.adapter.ChartVPAdapter;
import com.animee.tally.chart_frag.BaseChartFragment;
import com.animee.tally.chart_frag.ChartIncomeFragment;
import com.animee.tally.chart_frag.ChartOutcomeFragment;
import com.animee.tally.db.ChartItemBean;
import com.animee.tally.db.DBManager;
import com.animee.tally.util.CalenderDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/* 显示每个月的月账单*/
public class MonthChartActivity extends AppCompatActivity {
    Button inBtn,outBtn;
    ImageView backIv,calendarIv;
    TextView dateTv,inTv,outTv;
    ViewPager chartVp;
    int year,month;
    int selectPos = -1;
    int selectMonth = -1;
    int showKind = 0;   //默认显示的支出还是收入
    BaseChartFragment inchartFrag,outchartFrag;
    private FragmentManager manager;
    List<Fragment>chartFragmentList;
    private ChartVPAdapter chartVPAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_chart);
        initView();
        initDate();   //初始化时间的方法
        initStatistics(year,month);
        initFrag();
        setVPSelectListener();   //设置viewpager的监听事件
    }

    private void setVPSelectListener() {
        chartVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
               // 当页面发生改变时会回调的方法
              setButtonStyle(position);
            }
        });
    }

    private void initFrag() {
        chartFragmentList = new ArrayList<>();
        // 创建Fragment对象
        inchartFrag = new ChartIncomeFragment();
        outchartFrag = new ChartOutcomeFragment();
        // 添加数据到对象当中
        Bundle bundle = new Bundle();
        bundle.putInt("year",year);
        bundle.putInt("month",month);
        inchartFrag.setArguments(bundle);
        outchartFrag.setArguments(bundle);
        chartFragmentList.add(outchartFrag);
        chartFragmentList.add(inchartFrag);
        // 将Fragment加载到Activity当中
        manager = getSupportFragmentManager();
        chartVPAdapter = new ChartVPAdapter(manager, chartFragmentList);
        chartVp.setAdapter(chartVPAdapter);
    }

    /* 统计传入年月的收支情况*/
    private void initStatistics(int year,int month) {
        float inAccountOneMonth = DBManager.getSumAccountOneMonth(year, month, 1);
        float outAccountOneMonth = DBManager.getSumAccountOneMonth(year, month, 0);
        int incountOneMonth = DBManager.getCountAccountOneMonth(year, month, 1);
        int outcountOneMonth = DBManager.getCountAccountOneMonth(year, month, 0);
        dateTv.setText(year+"年"+month+"月账单");
        inTv.setText("共"+incountOneMonth+"笔收入，￥ "+inAccountOneMonth);
        outTv.setText("共"+outcountOneMonth+"笔支出，￥ "+outAccountOneMonth);
    }

    private void initDate() {
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
    }

    private void initView() {
        inBtn = findViewById(R.id.chart_btn_in);
        outBtn = findViewById(R.id.chart_btn_out);
        backIv = findViewById(R.id.chart_iv_back);
        calendarIv = findViewById(R.id.chart_iv_rili);
        dateTv = findViewById(R.id.chart_tv_date);
        inTv = findViewById(R.id.chart_tv_in);
        outTv = findViewById(R.id.chart_tv_out);
        chartVp = findViewById(R.id.chart_vp_bottom);
    }
    /*   设置按钮样式
    *    kind--1  点击了收入按钮
    *    kind==0  点击了支出按钮
    * */
    private void setButtonStyle(int kind){
        if (kind==1){
            inBtn.setBackgroundResource(R.drawable.main_btn_bg);
            inBtn.setTextColor(Color.WHITE);
            outBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            outBtn.setTextColor(Color.BLACK);
            showKind = 1;
        }else if (kind==0){
            outBtn.setBackgroundResource(R.drawable.main_btn_bg);
            outBtn.setTextColor(Color.WHITE);
            inBtn.setBackgroundResource(R.drawable.dialog_btn_bg);
            inBtn.setTextColor(Color.BLACK);
            showKind = 0;
        }
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.chart_iv_back:
                finish();
                break;
            case R.id.chart_iv_rili:
                CalenderDialog dialog = new CalenderDialog(this, selectPos, selectMonth);
                dialog.show();
                dialog.setDialogSize();
                dialog.setOnRefreshListener(new CalenderDialog.OnRefreshListener() {
                    @Override
                    public void onRefresh(int selpos, int selyear, int selmonth) {
                         selectPos = selpos;
                         selectMonth = selmonth;
                         initStatistics(selyear,selmonth);
                         inchartFrag.setDate(selyear,selmonth);
                         outchartFrag.setDate(selyear,selmonth);
                    }
                });
                break;
            case R.id.chart_btn_in:
                setButtonStyle(1);
                chartVp.setCurrentItem(1);
                break;
            case R.id.chart_btn_out:
                setButtonStyle(0);
                chartVp.setCurrentItem(0);
                break;
        }
    }


}
